package org.example.studentsdemo.student.service.impl;

import org.example.studentsdemo.student.entity.Students;
import org.example.studentsdemo.student.mapper.StudentMapper;
import org.example.studentsdemo.student.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    /**
     * @return
     */
    @Override
    public List<Students> getAllStudents() {
        try {

             List<Students> studentsList =studentMapper.selectList(null);
        return studentsList;
        }catch (Exception e){
            e.printStackTrace();
            return  null;
        }

    }

    /**
     * @param students
     * @return
     */
    @Override
    public Students saveStudent(Students students) {

      try {
           studentMapper.insert(students);
          return students;
      }catch (Exception e){
          // 处理异常，例如记录日志
          e.printStackTrace();
          return null;
      }

    }

    /**
     * @param id
     * @return
     */
    @Override
    public Students getStudentById(Long id) {
        try {
            return studentMapper.selectById(id);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param students
     * @return
     */
    @Override
    public Students updateStudent(Students students) {
        try {
            studentMapper.updateById(students);
            return students;
        } catch (Exception e) {
            // 记录异常日志，方便调试
            System.err.println("Error updating student: " + e.getMessage());
            return null;
        }
    }

    /**
     * @param id
     * @return
     */
    @Override
    public boolean deleteStudent(Long id) {
        try {
             studentMapper.deleteById(id);
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
